package com.cskaoyan.service;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminCommentServiceImpl implements AdminCommentService {
    @Override
    public BaseRespVo list(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCommentMapper mapper = sqlSession.getMapper(MarketCommentMapper.class);
        PageHelper.startPage(page,limit);

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketComment> marketComments = mapper.selectByExample(marketCommentExample);


        sqlSession.close();
        return BaseRespVo.ok(CommonData.data(new PageInfo(marketComments)));
    }

    @Override
    public BaseRespVo delete(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCommentMapper mapper = sqlSession.getMapper(MarketCommentMapper.class);
        int deleteLineNum = mapper.deleteByPrimaryKey(id);
        System.out.println(deleteLineNum);

        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok();
    }
}
